<?php
session_start();
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Buat update request
 * vote-nya pake:
 * /update.?pollingfield_id=blah&pollingoption_id=blah
 */

if(isset($_POST['pollingfield_id']) && isset($_POST['pollingoption_id']) && isset($_SESSION['auth']) && isset($_SESSION['userid']))
{
    $field_id=$_POST['pollingfield_id'];
    $optionid=$_POST['pollingoption_id'];
    $isauth=$_SESSION['auth'];
    $userid=$_SESSION['userid'];

    $cek=TRUE;

    // cek sudah vote
    require_once 'dbconnector.php';

    $query="SELECT * FROM hasvote WHERE pollingfield_id=$field_id AND userid=$userid;";
    $result=mysql_query($query);
    $rowcount=mysql_num_rows($result);
    if($rowcount!=0)
    {
        $cek=FALSE;
    }

    // validasi pollingfield_id
    $query="SELECT * FROM pollingfield WHERE id=$field_id";
    $result=mysql_query($query);
    $rowcount=mysql_num_rows($result);
    if($rowcount==0)
    {
        $cek=FALSE;
    }

    // validasi pollingoption_id
    $query="SELECT * FROM pollingoption WHERE id=$optionid";
    $result=mysql_query($query);
    $rowcount=mysql_num_rows($result);
    if($rowcount==0)
    {
        $cek=FALSE;
    }

    if($cek==TRUE)
    {
        $query="INSERT INTO hasvote (pollingfield_id,userid) VALUES ($field_id,$userid);";
        mysql_query($query);
        $query="SELECT * FROM pollingoption WHERE id=$optionid AND pollingfield_id=$field_id;";
        $rows=mysql_fetch_array(mysql_query($query));
        $count=$rows['jumlah'];
        $count++;
        $query="UPDATE pollingoption SET jumlah=$count WHERE id=$optionid AND pollingfield_id=$field_id;";
        mysql_query($query);
    }
}
header("Location: ../index.php");
?>
